package com.bjc.nio;

import java.nio.ByteBuffer;

/**
 * @program: nettypro
 * @description: 只读的buffer
 * @author: bjc
 * @create: 2021-09-03 21:48
 */
public class ReadOnlyBuffer {

    public static void main(String[] args) {
        //创建一个buffer
        ByteBuffer buffer = ByteBuffer.allocate(64);

        for (int i = 0; i < 10; i++) {
            buffer.put((byte) i);
        }

        //读取
        buffer.flip();

        //得到一个只读的Buffer
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
        System.out.println(readOnlyBuffer.getClass());

        //读取
        while (readOnlyBuffer.hasRemaining()){
            System.out.println(readOnlyBuffer.get());
        }

        //会抛异常 ReadOnlyBufferException
//        readOnlyBuffer.put((byte)100);
    }
}
